package com.wenhui.schoolyardinfo.square.model;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.wenhui.schoolyardinfo.manager.GsonManager;
import com.wenhui.schoolyardinfo.manager.Okhttp3Manager;
import com.wenhui.schoolyardinfo.square.bean.SquareRecommendBean;
import com.wenhui.schoolyardinfo.square.callback.OnSquareListener;
import com.wenhui.schoolyardinfo.square.model.base.ISquareModel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/9 17:03
 * introduce :
 */

public class SquareModelImpl implements ISquareModel {
    public static List<SquareRecommendBean> dataBeanList =new ArrayList<>();
    private Context context;
    private Okhttp3Manager okhttp3Manager;

    public SquareModelImpl(Context context) {
        this.context = context;
    }

    private SquareRecommendBean bean=new SquareRecommendBean();
    @Override
    public void getSquareData(Map<String, String> bodyParams, final OnSquareListener onSquareListener) {
        okhttp3Manager = Okhttp3Manager.getInstance(context);
        okhttp3Manager.postAsyncFormOkhttp3("192.168.31.96:8080/",
                null,
                bodyParams,
                new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        onSquareListener.onSquareError(e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        onSquareListener.onSquareSuccess(response.body().string());
                        Log.e("mhyresponse",response.toString());
                        // GSON直接解析成对象
                      GsonManager gsonManager= GsonManager.getInstance();
                        bean = gsonManager.fromJson(String.valueOf(response), SquareRecommendBean.class);//
                        //对象中拿到集合
                       dataBeanList.add(bean);
                    }
                });
    }

}
